//
//  TwoButtonsCell.m
//  Guazi
//
//  Created by koolearn on 11-5-29.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "TwoButtonsCell.h"
#import "ButtonsCellDelegate.h"


@implementation TwoButtonsCell

@synthesize delegate;
@synthesize button1;
@synthesize button2;
@synthesize context;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:button1];
        
        self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:button2];
    }
    return self;
}


- (void) setDelegate:(id)theDelegate
{
    [(id)delegate release];
    delegate = [theDelegate retain];
    
    NSString* title = [delegate titleForButton:button1.tag];
    button1.titleLabel.font = [UIFont boldSystemFontOfSize:13];
    [button1 setTitle:title forState:UIControlStateNormal];
    
    UIImage* image = [delegate imageForButton:button1.tag];
    [button1 setBackgroundImage:[image stretchableImageWithLeftCapWidth:0 topCapHeight:0] forState:UIControlStateNormal];
    
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    BOOL enabled = [delegate enabledForButton:button1.tag];
    [button1 setEnabled:enabled];
    if ( !enabled ) {
         [button1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    
    title = [delegate titleForButton:button2.tag];
    button2.titleLabel.font = [UIFont boldSystemFontOfSize:13];
    [button2 setTitle:title forState:UIControlStateNormal];
    
    image = [delegate imageForButton:button2.tag];
    [button2 setBackgroundImage:[image stretchableImageWithLeftCapWidth:0 topCapHeight:0] forState:UIControlStateNormal];
    
    [button2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    enabled = [delegate enabledForButton:button2.tag];
    [button2 setEnabled:enabled];
    if ( !enabled ) {
        [button2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
}


- (void) buttonClicked:(id)sender
{
    UIButton* button = (UIButton*)sender;
    [delegate didClickButton:button.tag withObject:context];
}


- (void) layoutSubviews
{
    CGRect bounds = self.contentView.bounds;
    int totalWitdh = 320 - 20;
   
    int width = ( totalWitdh - 30 ) / 2;
    int height = 50;
    
    int x = 10;
    int y = ( bounds.size.height - 44 ) / 2;
    
    button1.frame = CGRectMake(x, y, width, height);
    button2.frame = CGRectMake(x + width + x, y, width, height);
    [super layoutSubviews];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}


- (void)dealloc
{
    [button1 release];
    [button2 release];
    [(id)delegate release];
    [context release];
    [super dealloc];
}

@end
